// pages/withdraw/index.js
import config from '../../etc/config'

const {
  $Toast
} = require('../../components/dist/base/index');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    deId: '',
    disabled: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(e) {
    wx.hideShareMenu()
    this.setData({
      id: e.deId,
      canDrawMoney: e.canDrawMoney
    })
  },
  cashWithdrawal: function() {
    this.setData({
      money: this.data.canDrawMoney
    })
  },

  submitWithDraw(e) {
    console.log(e)
    if (!this.data.money) {
      $Toast({
        content: '提现金额不能为空'
      });
      return
    }
    this.setData({
      disabled: true
    })
    let formId = e.detail.formId
    let params = {
      deId: this.data.id,
      drawMoney: this.data.money,
      formId: formId,
    }
    let token = wx.getStorageSync('token')
    config._post('/deliverymandraw/drawByDelivery', token, params).then(res => {
      console.log(res)
      this.setData({
        disabled: false
      })
      if (res.code == 0) {
        wx.redirectTo({
          url: '../withdraw-result/index?',
          success: function(res) {},
          fail: function(res) {},
          complete: function(res) {},
        })
      }else {
        $Toast({
          content: res.msg
        });
      }
    })
  },
  moneyInput: function(e) {
    var money;
    if (/^[1-9]\d*$/.test(e.detail.value)) { //正则验证，提现金额小数点后不能大于两位数字
      money = e.detail.value;
    } else {
      money = e.detail.value.substring(0, e.detail.value.length - 1);
    }
    this.setData({
      money: money,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
})